function [A, mu, sigma, m, b] = FitPeak(x, y)
    %Fit to the standard Wikipedia definition of Gaussian. A = area, mu = center point, sigma = 1 stdev.
    %y = A / [sigma * sqrt(2*pi)] * exp(-1/2 * [x-mu]^2 / sigma^2)
    %Subtract off a background defined by a line.
    
    %if size of x and y are the wrong way, take the transpose.
    s = size(x); w = size(y);
    if s(1) < s(2)
        x = x';
    end
    if w(1) < w(2)
        y = y';
    end
    
    %lorentzFn = fittype('2*A1/pi * (w1./(4*(x-m1).^2 + w1.^2)) + 2*A2/pi * (w2./(4*(x-m2).^2 + w2^2)) + C');
    %gaussianFn = fittype('A / (sigma * sqrt(2*pi)) * exp(-1/2 * (x-mu)^2 / sigma^2) + m*x + b');
    gaussianFn = fittype('A / (sigma * sqrt(2*pi)) * exp(-1/2 * (x-mu)^2 / sigma^2)');

    %initialize the parameters.
    lineFit = fit([x(1); x(end)], [y(1); y(end)], 'poly1');
    m = lineFit.p1;
    b = lineFit.p2;
    mu = mean(x);
    sigma = range(x) / 6;
    A = (y(round(end/2)) - m*mean(x) - b) * range(x) / 5;
    
    
    %fitResult = fit(x', y', gaussianFn, 'StartPoint', [A, b, m, mu, sigma]);
    [fitResult, fitgoodness] = fit(x, y - (m*x + b), gaussianFn, 'StartPoint', [A, mu, sigma], 'Robust', 'LAR');
    
    level = 2*tcdf(-1, fitgoodness.dfe);
    confInterval = confint(fitResult, 1-level);
    A = [fitResult.A, (confInterval(2, 1) - confInterval(1, 1)) / 2];
    mu = [fitResult.mu, (confInterval(2, 2) - confInterval(1, 2)) / 2];
    sigma = [fitResult.sigma, (confInterval(2, 3) - confInterval(1, 3)) / 2];
    
    figure(1021);
    hold off;
    plot(x, y, '-o', 'color', [0,0,0]);
    hold all;
    plot(x, fitResult(x), 'color', 'red');
    plot(x, m*x+b, '--', 'color', [0.5, 0.5, 0.5]);
    plot(x, A / (sigma * sqrt(2*pi)) * exp(-1/2 * (x-mu).^2 / sigma.^2) + m*x + b);
    pause(0.5);

end